"""
28. 找出字符串中第一个匹配项的下标
https://leetcode.cn/problems/find-the-index-of-the-first-occurrence-in-a-string/
"""

# class Solution:
#     """直接使用字符串自带的index方法"""
#     def strStr(self, haystack: str, needle: str) -> int:
#         try:
#             return haystack.index(needle)
#         except ValueError:
#             return -1


class Solution:
    def strStr(self, haystack: str, needle: str) -> int:
        length = len(needle)
        size = len(haystack)

        if length > size:
            return -1

        for i in range(size):
             # 走到最后了，还是不对就退出
            if size - i < length:
                return -1

            if haystack[i] == needle[0]:
                if haystack[i:i+length] == needle:
                    return i


        return -1

def test():
    obj = Solution()
    res = obj.strStr("hello", "ll")
    print(res)


if __name__ == "__main__":
    test()
